/**
 * Xml.java
 * Version: 1.0
 * Created in 06/08/2013
 * Created by: Gerson Groth
 * 
 * Copyright
 */
package activities.base.xml;

import java.io.FileWriter;
import java.io.IOException;

import org.apache.log4j.Logger;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import webcrawler.activities.data.Data;

/**
 * The Class Xml.
 * 
 * @author Gerson Groth
 */
public class Xml {
	/** The logger. */
	private Logger logger = Logger.getLogger(Xml.class);

	/** The activities. */
	private Element activities = new Element("Activities");

	/** The activity. */
	private Element activity = null;

	/** The summary. */
	private Element summary = null;

	/** The details. */
	private Element details;

	/** The timing. */
	private Element timing;

	/** The elevation. */
	private Element elevation;

	/** The heart rate. */
	private Element heartRate;

	/** The cadence. */
	private Element cadence;

	/** The weather. */
	private Element weather;

	/** The additional info. */
	private Element additionalInfo;

	/** The laps. */
	private Element laps;

	/** The data. */
	private Data data;

	/** The all activities. */
	private Element allActivities = new Element("Activities");

	/**
	 * Instantiates a new xml.
	 * 
	 * @param data
	 *            the data
	 */
	public Xml(Data data) {
		this.data = data;
	}

	/**
	 * Next activity.
	 * 
	 * @param file
	 *            the file
	 * @param maxHR
	 *            the max hr
	 * @param user
	 *            the user
	 */
	public void nextActivity(String file, String maxHR, String user) {
		activity = new Element("Activity");
		activity.setAttribute("file", file);
		activity.setAttribute("maxHR", maxHR);
		activity.setAttribute("user", user);

		activities.addContent(activity.clone());
	}

	/**
	 * Save activity.
	 * 
	 * @param activityNumber
	 *            the i
	 */
	public void saveActivity(long activityNumber) {
		if (activity != null) {

			allActivities.addContent(activity.clone());

			Document doc = new Document();
			doc.setRootElement(activity);
			XMLOutputter xout = new XMLOutputter();
			System.err.print(" ");

			try {
				xout.setFormat(Format.getPrettyFormat().setEncoding(
						"ISO-8859-1"));
				xout.output(doc, new FileWriter(data.getXmlFolder()
						+ "activity_" + activityNumber + ".xml"));
			} catch (IOException ex) {
				logger.error("Error saving xml " + "activity_" + activityNumber + ".xml : ",
						ex);
			}
		}

	}

	/**
	 * Imprime xml.
	 */
	public void imprimeXML() {
		Document doc = new Document();
		Element newActivity = activities.clone();
		newActivity.setAttribute("numberOfActivities",
				Integer.toString(newActivity.getChildren().size()));
		doc.setRootElement(newActivity);

		XMLOutputter xout = new XMLOutputter();
		try {
			xout.setFormat(Format.getPrettyFormat());
			xout.output(doc, System.out);
			xout.output(doc, new FileWriter(data.getXmlFolder()
					+ "activities.xml"));
		} catch (IOException ex) {
			logger.error("Error saving xml " + "activities.xml : ", ex);
		}

		doc = new Document();

		Element newAllActivity = allActivities.clone();
		newAllActivity.setAttribute("numberOfActivities",
				Integer.toString(newAllActivity.getChildren().size()));
		doc.setRootElement(newAllActivity);

		xout = new XMLOutputter();
		try {
			xout.setFormat(Format.getPrettyFormat().setEncoding("ISO-8859-1"));
			xout.output(doc, new FileWriter(data.getXmlFolder()
					+ "activitiesAll.xml"));
		} catch (IOException ex) {
			logger.error("Error saving xml " + "activitiesAll.xml : ", ex);
		}

	}

	/**
	 * Adds the header.
	 * 
	 * @param activityName
	 *            the activity name
	 * @param timestamp
	 *            the timestamp
	 * @param username
	 *            the username
	 * @param activityTypeValue
	 *            the activity type value
	 * @param eventTypeValue
	 *            the event type value
	 * @param courseValue
	 *            the course value
	 */
	public void addHeader(String activityName, String timestamp,
			String username, String activityTypeValue, String eventTypeValue,
			String courseValue) {
		Element header = new Element("HeaderContent");

		Element activityNameElement = new Element("ActivityName");
		activityNameElement.setText(activityName);

		Element timestampElement = new Element("Timestamp");
		timestampElement.setText(timestamp);

		Element userElement = new Element("UserName");
		userElement.setText(username);

		Element activityType = new Element("ActivityType");
		activityType.setText(activityTypeValue);

		Element eventType = new Element("EventType");
		eventType.setText(eventTypeValue);

		Element course = new Element("Course");
		course.setText(courseValue);

		header.addContent(activityNameElement);
		header.addContent(timestampElement);
		header.addContent(userElement);
		header.addContent(activityType);
		header.addContent(eventType);
		header.addContent(course);
		activity.addContent(header);
	}

	/**
	 * Creates the summary.
	 */
	public void createSummary() {
		summary = new Element("Summary");
		activity.addContent(summary);
	}

	/**
	 * Adds the summary.
	 * 
	 * @param name
	 *            the name
	 * @param value
	 *            the value
	 */
	public void addSummary(String name, String value) {
		Element element = new Element(name);
		element.setText(value);
		summary.addContent(element);
	}

	/**
	 * Creates the details.
	 */
	public void createDetails() {
		details = new Element("Details");
		timing = new Element("Timing");
		elevation = new Element("Elevation");
		heartRate = new Element("HeartRate");
		cadence = new Element("Cadence");

		details.addContent(timing);
		details.addContent(elevation);
		details.addContent(heartRate);
		details.addContent(cadence);
		activity.addContent(details);
	}

	/**
	 * Adds the timing.
	 * 
	 * @param name
	 *            the name
	 * @param value
	 *            the value
	 */
	public void addTiming(String name, String value) {
		Element element = new Element(name);
		element.setText(value);
		timing.addContent(element);
	}

	/**
	 * Adds the elevation.
	 * 
	 * @param name
	 *            the name
	 * @param value
	 *            the value
	 */
	public void addElevation(String name, String value) {
		Element element = new Element(name);
		element.setText(value);
		elevation.addContent(element);
	}

	/**
	 * Adds the heart hate.
	 * 
	 * @param name
	 *            the name
	 * @param value
	 *            the value
	 */
	public void addHeartHate(String name, String value) {
		Element element = new Element(name);
		element.setText(value);
		heartRate.addContent(element);
	}

	/**
	 * Adds the cadence.
	 * 
	 * @param name
	 *            the name
	 * @param value
	 *            the value
	 */
	public void addCadence(String name, String value) {
		Element element = new Element(name);
		element.setText(value);
		cadence.addContent(element);
	}

	/**
	 * Creates the weather.
	 */
	public void createWeather() {
		weather = new Element("Weather");
		activity.addContent(weather);
	}

	/**
	 * Adds the weather.
	 * 
	 * @param name
	 *            the name
	 * @param value
	 *            the value
	 */
	public void addWeather(String name, String value) {
		Element element = new Element(name);
		element.setText(value);
		weather.addContent(element);
	}

	/**
	 * Creates the additional info.
	 */
	public void createAdditionalInfo() {
		additionalInfo = new Element("AdditionalInfo");
		activity.addContent(additionalInfo);
	}

	/**
	 * Adds the additional info.
	 * 
	 * @param name
	 *            the name
	 * @param value
	 *            the value
	 */
	public void addAdditionalInfo(String name, String value) {
		Element element = new Element(name);
		element.setText(value);
		additionalInfo.addContent(element);
	}

	/**
	 * Creates the laps.
	 * 
	 * @param nrLaps
	 *            the number of laps
	 */
	public void createLaps(String nrLaps) {
		laps = new Element("Laps");
		laps.setAttribute("nrLaps", nrLaps);
		activity.addContent(laps);
	}

	/**
	 * Adds the lap.
	 * 
	 * @param split
	 *            the split
	 * @param time
	 *            the time
	 * @param distance
	 *            the distance
	 * @param avgPace
	 *            the average pace
	 */
	public void addLap(String split, String time, String distance,
			String avgPace) {
		Element lap = new Element("Lap");
		lap.setAttribute("split", split);
		lap.setAttribute("time", time);
		lap.setAttribute("distance", distance);
		lap.setAttribute("avgPace", avgPace);
		laps.addContent(lap);
	}
}
